/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.master.app;

import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.dto.CommonResult;
import org.openislands.oi.pojo.dto.JobDTO;
import org.openislands.oi.master.scheduler.DAGScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JobService {
    @Resource
    private DAGScheduler dagScheduler;

    public JobDTO submitJob(JobDTO jobDTO){
        return dagScheduler.submitJob(jobDTO);
    }

    public CommonResult<Boolean> schedulingJobs(){
        try{
            dagScheduler.schedulingJobs();
            return CommonResult.success();
        }catch (LogicException e){
            return CommonResult.error(e.getResponseMessageCode());
        }
    }

    public CommonResult<Boolean> handleJob(String jobId){
        try{
            dagScheduler.handleJob(jobId);
            return CommonResult.success();
        }catch (LogicException e){
            return CommonResult.error(e.getResponseMessageCode());
        }
    }

    public CommonResult<Boolean> stopJob(String jobId){
        try{
            boolean st = dagScheduler.stopJob(jobId);
            return st ? CommonResult.success() : CommonResult.error(MessageCodeEnum.EXECUTION_ERROR);
        }catch (LogicException e){
            return CommonResult.error(e.getResponseMessageCode());
        }
    }
}
